package org.itboys.weixin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.fuzion.entity.WithdrawsRecords;
import org.itboys.fuzion.mysql.orm.FuziongMemberStatistics;
import org.itboys.fuzion.mysql.service.FuziongMemberStatisticsService;
import org.itboys.fuzion.service.WithdrawsRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
/**
 * 微信端提现
 * @author weisky
 *
 * 2016年4月19日
 */
@RestController 
@RequestMapping("/weixin/withdrawsRecords") 
public class WeixinWithdrawsRecordsController extends BaseController{
	
	@Autowired
	private WithdrawsRecordsService withdrawsRecordsService;
	@Autowired
	private FuziongMemberStatisticsService fuziongMemberStatisticsService;
	
	/**
	 * 到提现页面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toWithdrawsVM")
	public ModelAndView toListVM(Model model,
			HttpServletRequest request, HttpServletResponse response) {
		Long memberId = (Long) request.getSession().getAttribute("memberId");
		
		FuziongMemberStatistics fms = fuziongMemberStatisticsService.getByMember(memberId);
		if(fms != null){
			model.addAttribute("score", fms.getNowTotalMoney()/100);//获取当前可用积分  除以100 由分转化为元
		}
		
		List<WithdrawsRecords> list = withdrawsRecordsService.getListByMemberId(memberId);
		model.addAttribute("record", list);//获取历史审核通过的提现记录
		
		return new ModelAndView("/site/withdraws");
	}
	
	/**
	 * 提现操作
	 * @param request
	 * @param response
	 */
	@RequestMapping("/doWithdraws")
	public void doWithdraws(HttpServletRequest request, HttpServletResponse response){
		Long memberId = (Long) request.getSession().getAttribute("memberId");
		if(memberId != null){
			FuziongMemberStatistics fms = fuziongMemberStatisticsService.getByMember(memberId);
			if(fms.getNowTotalMoney() <= 0){
				AjaxUtils.renderText(response, "-1");
				return ;
			}
			WithdrawsRecords wr = new WithdrawsRecords();
			wr.setMemberId(memberId);
			wr.setMoney(fms.getNowTotalMoney());
			boolean result = withdrawsRecordsService.insert(wr);
			if(result){
				AjaxUtils.renderText(response, CommonConstants.SUCCESS);
				return ;
			}
		}
		AjaxUtils.renderText(response, CommonConstants.FAIL);
	}

}
